package database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import database.DatabaseUtility.DatabaseHelper;

public class DataRecordDAO {
	private static final String DATA_RECORD = "DATA_RECORD";
	private static final String DATABASE_NAME = "FTICO_DATABASE";
	private static final int DATABASE_VERSION = 1;

	private static final String DATA_RECORD_ID = "DATA_RECORD_ID";
	private static final String DATA_SERIE = "DATA_SERIE";
	private static final String TIME_OF_DEVICE = "TIME_OF_DEVICE";
	private static final String NUMBER_OF_VALUE = "NUMBER_OF_VALUE";
	private static final String V1 = "V1";
	private static final String V2 = "V2";
	private static final String V3 = "V3";
	private static final String V4 = "V4";
	private static final String V5 = "V5";
	private static final String V6 = "V6";
	private static final String V7 = "V7";
	private static final String V8 = "V8";
	private static final String V9 = "V9";
	private static final String V10 = "V10";
	private static final String CREATE_AT = "CREATE_AT";
	private static final String UPDATE_AT = "UPDATE_AT";
	public static final String DELETE_FLG = "DELETE_FLG";

	private Context mContext;
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDB;

	public DataRecordDAO(Context ctx) {
		mContext = ctx;
	}

	public DataRecordDAO open() {
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDbHelper.set_mContext(mContext);
		mDB = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void update() {
		mDbHelper.onUpgrade(mDB, DATABASE_VERSION, 2);
	}

	public void insertDataRecord(int v1, int v2, int v3, String time) {
		ContentValues values = new ContentValues();
		values.put(V1, v1);
		values.put(V2, v2);
		values.put(V3, v3);
		values.put(V4, 0);
		values.put(V5, 0);
		values.put(V6, 0);
		values.put(V7, 0);
		values.put(V8, 0);
		values.put(V9, 0);
		values.put(V10, 0);
		values.put(DATA_SERIE, 0);
		values.put(NUMBER_OF_VALUE, 0);
		values.put(TIME_OF_DEVICE, time);
		values.put(CREATE_AT, "unknown");
		values.put(UPDATE_AT, "unknown");
		values.put(DELETE_FLG, 0);
		mDB.insert(DATA_RECORD, null, values);
	}


	public int getSize() {
		int size = 0;
		Cursor cursor = mDB.query(DATA_RECORD, null, "DELETE_FLG = 0", null,
				null, null, null);
		if (cursor.moveToFirst()) {
			do {
				++size;
			} while (cursor.moveToNext());
		}
		return size;
	}
}
